home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3.iso / chapte16 / connect.c next >
C/C++ Source or Header  |  1996-04-28  |  8KB  |  260 lines

  1.  
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include "connect.h"
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "midiConnect()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW | WS_VSCROLL, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.     WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106. #define MSG_LEN          1024
  107.  
  108. // global variables
  109. //.................
  110.  
  111. char     msg[MSG_LEN+1];
  112.  
  113. HWND     hListBox = NULL;
  114. MMRESULT rc;
  115.  
  116. UINT     nDevId = 0;
  117. HMIDIIN  hmi = NULL;
  118. HMIDIOUT hmo = NULL;
  119.  
  120.  
  121. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  122. {
  123.    switch( uMsg )
  124.    {
  125.       case WM_CREATE :
  126.               // Create listbox
  127.               //...............
  128.  
  129.               hListBox = CreateWindow( "LISTBOX", "",    
  130.                                        WS_CHILD | LBS_NOTIFY | WS_VSCROLL | WS_BORDER | 
  131.                                        WS_VISIBLE | LBS_NOINTEGRALHEIGHT, 
  132.                                        0, 0, 
  133.                                        0, 0,  
  134.                                        hWnd,              
  135.                                        (HMENU)101,              
  136.                                        hInst,         
  137.                                        NULL );
  138.               break;
  139.  
  140.       case WM_SIZE :
  141.               MoveWindow( hListBox, 0, 0, 
  142.                           LOWORD( lParam ), 
  143.                           HIWORD( lParam ), TRUE );
  144.               break;
  145.  
  146.       case WM_COMMAND :
  147.               switch( LOWORD( wParam ) )
  148.               {
  149.                  case IDM_TEST :
  150.                         {
  151.                            // open MIDI input device
  152.                            //.......................
  153.  
  154.                            rc = midiInOpen(&hmi, nDevId, (DWORD)NULL, 
  155.                                            (DWORD)NULL, CALLBACK_NULL);
  156.  
  157.                            if (rc != MMSYSERR_NOERROR)
  158.                            {
  159.                                midiInGetErrorText(rc, msg, MSG_LEN);
  160.                                MessageBox(hWnd, msg, NULL, MB_OK);
  161.                                break;
  162.                            }
  163.  
  164.                            // open MIDI output device
  165.                            //........................
  166.  
  167.                            rc = midiOutOpen(&hmo, nDevId, (DWORD)NULL, 
  168.                                             (DWORD)NULL, CALLBACK_NULL);
  169.  
  170.                            if (rc != MMSYSERR_NOERROR)
  171.                            {
  172.                                midiInClose(hmi);
  173.                                midiOutGetErrorText(rc, msg, MSG_LEN);
  174.                                MessageBox(hWnd, msg, NULL, MB_OK);
  175.                                break;
  176.                            }
  177.                            
  178.                            // connect MIDI input device to 
  179.                            // MIDI output device
  180.                            //.............................
  181.  
  182.                            rc = midiConnect(hmi, hmo, NULL);
  183.  
  184.                            if (rc != MMSYSERR_NOERROR)
  185.                            {
  186.                                midiInClose(hmi);
  187.                                midiOutClose(hmo);
  188.                                midiOutGetErrorText(rc, msg, MSG_LEN);
  189.                                MessageBox(hWnd, msg, NULL, MB_OK);
  190.                                break;
  191.                            }
  192.  
  193.                            // all data received from the MIDI input device
  194.                            // will be sent to the MIDI output device
  195.                            //.............................................
  196.  
  197.                            // do something here...
  198.                            
  199.                            // disconnect MIDI devices
  200.                            //........................
  201.  
  202.                            midiDisconnect(hmi, hmo, NULL);
  203.  
  204.                            // close both MIDI devices
  205.                            //........................
  206.  
  207.                            midiInClose(hmi);
  208.                            midiOutClose(hmo);
  209.                         }
  210.                         break;
  211.  
  212.                  case IDM_ABOUT :
  213.                         DialogBox( hInst, "AboutBox", hWnd, About );
  214.                         break;
  215.  
  216.                  case IDM_EXIT :
  217.                         DestroyWindow( hWnd );
  218.                         break;
  219.               }
  220.               break;
  221.  
  222.       case WM_DESTROY :
  223.               PostQuitMessage(0);
  224.               break;
  225.  
  226.       default :
  227.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  228.    }
  229.  
  230.    return( 0L );               
  231. }
  232.  
  233.  
  234. LRESULT CALLBACK About( HWND hDlg,           
  235.                         UINT message,        
  236.                         WPARAM wParam,       
  237.                         LPARAM lParam)
  238. {
  239.    switch (message) 
  240.    {
  241.        case WM_INITDIALOG: 
  242.                return (TRUE);
  243.  
  244.        case WM_COMMAND:                              
  245.                if (   LOWORD(wParam) == IDOK         
  246.                    || LOWORD(wParam) == IDCANCEL)    
  247.                {
  248.                        EndDialog(hDlg, TRUE);        
  249.                        return (TRUE);
  250.                }
  251.                break;
  252.    }
  253.  
  254.    return (FALSE); 
  255. }
  256.  
  257.  
  258.  
  259.  
  260.